package com.mrtan.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrtan.bean.Model;
import com.mrtan.bean.Product;
import com.mrtan.dto.PageBeanDto;
import com.mrtan.service.ModelService;
import com.mrtan.service.ProductService;
import com.mrtan.vo.ModelVO;
import com.mrtan.vo.ProductCountVo;
import com.mrtan.vo.ProductWhereVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品
 * @author 李聪
 * @version 2023/12/8
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ModelService modelService;
    @Resource
    private ProductService productService;

    @PostMapping("/class")
    public SaResult allclass(@RequestBody ModelVO modelVO) {
        PageHelper.startPage(modelVO.getPage(), modelVO.getLimit());
        List<Model> byName = modelService.findByName(modelVO.getName());
        PageInfo<Model> pageInfo = new PageInfo<>(byName);
        PageBeanDto<Model> pageBeanDto = new PageBeanDto();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }

    @GetMapping("/getclass")
    public SaResult getclass() {
        List<Model> byName = modelService.findByName(null);
        return SaResult.ok().setData(byName);
    }

    @DeleteMapping("/remove/{id}")
    public SaResult allClass(@PathVariable int id) {
        modelService.removeById(id);
        return SaResult.ok();
    }

    @GetMapping("/add")
    public SaResult addClass(@RequestParam String name){
        System.out.println(name);
        int i = modelService.addModel(name);
        if(i>0){
            return SaResult.ok("添加成功");
        }
        else {
            return SaResult.error("添加失败");
        }
    }
    @PostMapping("/allproduct")
    public SaResult allProduct(@RequestBody ProductWhereVO productWhereVO){
        PageHelper.startPage(productWhereVO.getPage(), productWhereVO.getLimit());
        List<Product> all = productService.findAll(productWhereVO);
        PageInfo<Product> pageInfo = new PageInfo<>(all);
        PageBeanDto<Product> pageBeanDto = new PageBeanDto();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }

    @PostMapping("/addproduct")
    public SaResult addProduct(@RequestBody ProductWhereVO productWhereVO){
        int i = productService.addProduct(productWhereVO);
        if(i<=0){
            return SaResult.error("新增失败，请检查你所填写的内容");
        }
        return SaResult.ok();
    }
    @DeleteMapping("/removeproduct/{id}")
    public SaResult removeproduct(@PathVariable int id) {
        int i = productService.deleteProduct(id);
        if(i<=0){
            return SaResult.error("删除失败");
        }
        return SaResult.ok();
    }

    @PostMapping("/getCount")
    public SaResult getCount(@RequestBody ProductCountVo productCountVo) {
        List<Object[]> list=productService.getCount(productCountVo.getModel());
        return SaResult.ok().setData(list);
    }
}
